package approval

import (
	"data/config"
	"data/view/common"
	"data/view/team"
	"data/view/user"
	"encoding/json"
	"public/mysqldb"
	"strings"
)

//获取下一个审批人
func getNextApprover(current, approvers string) (is_last bool, next string) {
	array := strings.Split(approvers, common.SPLIT)
	if len(array) > 0 {
		var current_loc int //当前位置
		for k, v := range array {
			if v == current {
				current_loc = k
			}
		}
		if current_loc == len(array)-1 {
			is_last = true
		} else {
			next = array[current_loc+1]
		}
	}
	return
}

//获取抄送人全部审批模板id
func getCopyerList(coper string) (ref_id []int) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	//抄送人占位符
	var clist []Approval_copyer_tbl
	orm.Where("copyer = ?", coper).Find(&clist)
	if len(clist) > 0 {
		for _, v := range clist {
			ref_id = append(ref_id, v.Ref_id)
		}
	}
	//抄送人确定
	var list []Approval_list_tbl
	orm.Where("copyers like ?", "%"+coper+"%").Find(&list)
	if len(list) > 0 {
		for _, v := range list {
			var copers = strings.Split(v.Copyers, common.SPLIT)
			for _, j := range copers {
				if j == coper {
					ref_id = append(ref_id, int(v.Id))
					break
				}
			}
		}
	}
	return
}

//获取审批提交内容
func GetApprovalContent(content string) (mp map[string]Widget_info) {
	var widget []Widget_info
	json.Unmarshal([]byte(content), &widget)

	if len(widget) > 0 {
		mp = make(map[string]Widget_info)
		for _, v := range widget {
			mp[v.Name] = v
		}
	}
	return
}

//创建审批人和抄送人
func CreateApproversAndCopers(approvers, copers []string, applicant, apply_no, parent_user string, ref_id int) bool {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	if len(approvers) > 0 {

		for k, v := range approvers {
			var tmp Approval_result_tbl
			tmp.Applicant = applicant
			tmp.Apply_no = apply_no
			if v == common.PLACE_HOLDER { //占位符
				tmp.Approver = parent_user //直接上级
			} else {
				tmp.Approver = v
			}
			if k == 0 {
				tmp.Is_current = 1 //当前审批人
			}
			err := orm.Create(&tmp).Error
			if err != nil {
				return false
			}
		}
	}
	if len(copers) > 0 {
		for _, v := range copers {
			if v == common.PLACE_HOLDER { //占位符
				var tmp Approval_copyer_tbl
				tmp.Applicant = applicant
				tmp.Apply_no = apply_no
				tmp.Ref_id = ref_id
				tmp.Copyer = parent_user //直接上级
				err := orm.Create(&tmp).Error
				if err != nil {
					return false
				}
			}

		}
	}
	return true

}

//获取模板审批人和抄送人
func GetApproversAndCopers(approvers_array, copers_array []string, current_user string) (approvers, copyers []User_info_tmp) {
	all_user, _ := user.GetAllUserDetail()

	var parent string //团队长
	if len(team.OnGetTeamBasicInfoByUser(current_user).Leader) > 0 {
		parent = team.OnGetTeamBasicInfoByUser(current_user).Leader[0].User
	}
	//审批人
	if len(approvers_array) > 0 {
		for _, v := range approvers_array {
			var t User_info_tmp
			if v == common.PLACE_HOLDER {
				t.User = parent
			} else {
				t.User = v
			}
			t.Alias = all_user[t.User].Alias
			t.Headimg_url = all_user[t.User].Headimg_url
			approvers = append(approvers, t)
		}
	}
	//抄送人
	if len(copers_array) > 0 {
		for _, v := range copers_array {
			var t User_info_tmp
			if v == common.PLACE_HOLDER {
				t.User = parent
			} else {
				t.User = v
			}
			t.Alias = all_user[t.User].Alias
			t.Headimg_url = all_user[t.User].Headimg_url
			copyers = append(copyers, t)
		}
	}
	return
}

//获取抄送人和审批结果
func GetCopersAndResult(copers_array []string, applicant, apply_no string) (copyers []User_info_tmp, approval_result []Approval_result) {
	var db mysqldb.MySqlDB
	defer db.OnDestoryDB()
	orm := db.OnGetDBOrm(config.GetDbUrl())

	var parent string //团队长
	if len(team.OnGetTeamBasicInfoByUser(applicant).Leader) > 0 {
		parent = team.OnGetTeamBasicInfoByUser(applicant).Leader[0].User
	}

	all_user, _ := user.GetAllUserDetail()
	if len(copers_array) > 0 {
		for _, v := range copers_array {
			var t User_info_tmp
			if v == common.PLACE_HOLDER {
				t.User = parent
			} else {
				t.User = v
			}
			t.Alias = all_user[t.User].Alias
			t.Headimg_url = all_user[t.User].Headimg_url
			copyers = append(copyers, t)
		}
	}
	var tmp []Approval_result
	orm.Table("approval_result_tbl").Where("apply_no = ?", apply_no).Find(&tmp)
	if len(tmp) > 0 {
		for _, v := range tmp {
			var t Approval_result
			t.Approval_time = v.Approval_time
			t.Approver = v.Approver
			t.Comment = v.Comment
			t.Is_current = v.Is_current
			t.Result = v.Result
			t.Alias = all_user[v.Approver].Alias
			t.Headimg_url = all_user[v.Approver].Headimg_url
			approval_result = append(approval_result, t)
		}
	}

	return
}
